<?php
require_once ("db/db_connector.php");
require_once ("db/db_paradise.php");
require_once ("db/db_picture.php");
require_once ("db/db_video.php");
require_once ("db/db_review.php");
require_once ("db/db_tag.php");
require_once ("db/db_favorite.php");

if (!isset($_GET["id"])) {
	exit("Error : Paradise ID has not been provided...");
}

if (isset($_GET["error"])) {
	$error_msg = "Failed to add";
}

define("PICTURE_COUNT", 5);
define("VIDEO_COUNT", 4);

$id = $_GET["id"];

// Database Access Begins ---------------------------------------- //
$con = get_db_con();
$paradise = get_paradise($con, $id);
$picture_list = get_picture_list($con, $id, PICTURE_COUNT);
$video_list = get_video_list($con, $id, VIDEO_COUNT);
$review_list = get_review_list($con, $id);
$tag_list = get_paradise_tags($con, $id);
close_db_con();
// Database Access Ends ------------------------------------------ //
?>

<?php
include_once ("include/header.php");
?>

<h1><?=$paradise["name"] ?>

</h1>
<div>
	<img src="<?=$paradise["thumb_big"] ?>" style="float:left;" />
	<table id="paradise_profile">
		<tr><th>Name</th><td><?=$paradise["name"] ?></td></tr>
		<tr><th>Rating</th><td><?=$paradise["rating"] ?></td></tr>
		<tr><th>Highest Temperature</th><td><?=$paradise["climate_high"] ?></td></tr>
		<tr><th>Lowest Temperature</th><td><?=$paradise["climate_low"] ?></td></tr>
		<tr><th>Language</th><td><?=$paradise["language"] ?></td></tr>
		<tr><th>Religion</th><td><?=$paradise["religion"] ?></td></tr>
		<tr><th>Population</th><td><?=$paradise["population"] ?></td></tr>
		<tr><th>GDP</th><td><?=$paradise["gdp"] ?></td></tr>
		<tr><th>Description</th><td><?=$paradise["climate"] ?></td></tr>
		<input id="longitude" type="hidden" value=<?=$paradise["location_long"] ?>>
		<input id="latitude" type="hidden" value=<?=$paradise["location_lat"] ?>>
		<?php
		function cmp($tag1, $tag2) {
			$a = $tag1['rating'];
			$b = $tag2['rating'];
			if ($a == $b) {
				return 0;
			}
			return ($a < $b) ? 1 : -1;
		}
		usort($tag_list,"cmp");
		?>

		<tr><th>Tags</th>
			<td>
				<table id="tag_table">
					<tr><th align="left">Tag Name</th><th align="left">Tag Rating</tr></tr>
					<?php foreach ($tag_list as $tag) :?>
						<tr>
							<td><?=$tag['name'] ?></td><td><?=$tag["rating"] ?></td> 
						</tr>
					<?php endforeach ?>
				</table>
			</td>
		</tr>
		<?php if (isset($_SESSION['user'])) :?>
			<tr>	
				<th>Add Tag</th>
				<td>
					<form action="tag_end.php" method="post">
						<input type="hidden" name="paradise_id" value="<?=$id ?>">
						<p>Tag: <input type="text" name="name"></p>
						<input type="submit" value="Add Tag">
					</form>
				</td>
		</tr>
		<?php endif ?>
	</table>
	<?php if (isset($_SESSION["user"])) :?>
	<form action="favorite_end.php" method="post">
		<input type="hidden" name="paradise_id" value="<?=$id ?>">
		<?php if (is_favorite($id)) :?>
			<input type="submit" value="Un-Favorite" />
		<?php else : ?>
			<input type="submit" value="Favorite" />
		<?php endif ?>
	</form>
	<?php endif ?>

</div>
<div style="clear:both;"></div>

<a name="pictures" />
<h2>Pictures <small>(<a href="paradise_gallery.php?id=<?=$paradise["id"] ?>&picture=1&video=0">More</a>)</small></h2>
<p>Showing most recent <?=PICTURE_COUNT ?> pictures. Click "More" button to see all pictures.</p>
<div>
	<?php foreach ($picture_list as $picture) :?>
		<div style="float:left; margin-right:1em;">
			<img src="<?=$picture["thumb_small"] ?>" />
			<div style="text-align:center">
				<a href="<?=$picture["url"] ?>" class="lightbox slides">
					<?=$picture["title"] ?>
				</a>
			</div>
		</div>
	<?php endforeach ?>
</div>
<div style="clear:both;"></div>

<?php if(isset($_SESSION["user"])) :?>
	<h3>Add Picture</h3>
	<form action="picture_edit_end.php" method="post" enctype="multipart/form-data">
		<input type="hidden" name="user_id" value="<?=$_SESSION["user_id"] ?>" />
		<input type="hidden" name="paradise_id" value="<?=$id ?>" />
		<div>
			<label for="title">Title : </label>
			<td><input id="title" name="title" type="text" /></td>
		</div>
		<div>
			<label for="picture">File to Upload</label>
			<input name="picture" id="picture" type="file" accept="image/*" />
		</div>
		<button type="submit">Add</button>
	</form>
<?php endif ?>
<div style="clear:both;"></div>

<a name="videos" />
<h2>Videos <small>(<a href="paradise_gallery.php?id=<?=$paradise["id"] ?>&picture=0&video=1">More</a>)</small></h2>
<p>Showing most recent <?=VIDEO_COUNT ?> videos. Click "More" button to see all videos.</p>
<div>
	<?php foreach ($video_list as $video) :?>
		<div style="float:left; margin-right:1em;">
			<img src="http://img.youtube.com/vi/<?=$video["youtube_id"] ?>/mqdefault.jpg" />
			<div style="text-align:center">
				<a href="http://www.youtube.com/embed/<?=$video["youtube_id"] ?>?rel=0&amp;wmode=transparent" class="youtube slides">
					<?=$video["title"] ?>
				</a>
			</div>
		</div>
	<?php endforeach ?>
</div>
<<div style="clear:both;"></div>

<?php if(isset($_SESSION["user"])) :?>
	<h3>Add Video</h3>
	<form action="video_edit_end.php" method="post">
		<input type="hidden" name="user_id" value="<?=$_SESSION["user_id"] ?>" />
		<input type="hidden" name="paradise_id" value="<?=$id ?>" />
		<div>
			<label for="title">Title : </label>
			<td><input id="title" name="title" type="text" /></td>
		</div>
		<div>
			<label for="youtube_id">Youtube ID : </label>
			<input id="youtube_id" name="youtube_id" type="text" />
		</div>
		<button type="submit">Add</button>
	</form>
	<?php if(isset($error_msg)) : ?>
		<div><span><?=$error_msg ?></span></div>
	<?php endif ?>
<?php endif ?>
<div style="clear:both;"></div>

<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
  	function initialize() {
  	var lat = <?=$paradise["location_lat"] ?>
	;
	var longitude =  
 <?=$paradise["location_long"] ?>
		;

		var map_canvas = document.getElementById('map_canvas');
		var map_options = {
			center : new google.maps.LatLng(lat, longitude),
			zoom : 8,
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(map_canvas, map_options);
		}
		google.maps.event.addDomListener(window, 'load', initialize); 
</script>
<div id="map_canvas" style="width:500px;height:400px;"></div>

<a name="reviews" />
<h2>Reviews</h2>

<?php if(isset($_SESSION["user"])) :?>
	<h3>Write Review</h3>
	<form action="review_edit_end.php" method="post">
		<input type="hidden" name="user_id" value="<?=$_SESSION["user_id"] ?>" />
		<input type="hidden" name="paradise_id" value="<?=$id ?>" />
		<label for="review">Content</label>
		<textarea name="content" id="content"></textarea>
		<div style="clear:both;"></div>
		<button type="submit">Post</button>
	</form>
<?php endif ?>

<ul>
	<?php foreach ($review_list as $review) :?>
		<li>
			<p><?=$review["content"] ?></p>
		</li>
	<?php endforeach ?>
</ul>
<div style="clear:both;"></div>

<?php
include_once ("include/footer.php");
?>
